clear all
set more off, perm
local mydir "`1'"
global Tables	`mydir'/XS/Tables
global Work	`mydir'/XS/DataWork
global Raw	`mydir'/XS/DataRaw
global WRDS	`mydir'/WRDS
/* ********************************************************************* */
* Get data on market cap and returns; compute vw return and mcap of the firm
* Keep the permno of the security with the largest mcap
* msf_adjusted.dta generated by read_crsp.do
use if ret<. using $Work/msf_adjusted, clear

* Create weight to compute vw return and collapse data at the firm level
* deal with obs with missing mcap data (mostly IPOs; adjustment plays no role for 98.55% with only one share class).
egen id=group(permco permno) 
tsset id datem

generate Lmcap=L.mcap 	if L.mcap>0
replace  Lmcap=mcap 	if Lmcap==. & mcap>0
keep if ret+Lmcap<.
/* ********************************************************************* */
preserve
* figure out how many firms have multiple classes of shares
duplicates tag permco datem, gen(dup)
tabulate dup

* when firm has multiple securities, compute vw return; sort data so that i select the permno for the security with the largest market cap @t
sort permco datem mcap
collapse (lastnm) comnam permno siccd exchcd date (sum) Mcap=mcap (mean) ret [aweight=Lmcap] , by(permco datem)

tsset permco datem
forvalues x=1(1)60 {
	quietly: generate   ret`x'=F`x'.ret 
	quietly: generate  L`x'ret=L`x'.ret 
	quietly: generate F`x'Mcap=F`x'.Mcap 
	quietly: generate L`x'Mcap=L`x'.Mcap 
}
rename Mcap F0Mcap
rename ret  ret0
order *, sequential

keep  datem date comnam permco permno siccd exchcd  F0Mcap-F60Mcap L1Mcap-L60Mcap ret0-ret60 L*ret
order datem date comnam permco permno siccd exchcd  F0Mcap-F60Mcap L1Mcap-L60Mcap ret0-ret60 L1ret-L60ret
* *******************************
save $Work/RET, replace
* *******************************
restore
/* ********************************************************************* */
collapse (sum) Lmcap , by(permco datem)
merge 1:1 permco datem using $Work/RET
drop _merge
* *****************************************************
* size deciles using NYSE deciles
bys datem: astile decile = F0Mcap if month(dofm(datem))==6, qc(exchcd==1) nq(10)
tsset permco datem
replace decile=L.decile if decile==. & month(dofm(datem))!=6 

* find size cutoff for NYSE firms and data for June
bys datem: egen median=median(F0Mcap)	if exchcd==1 & month(dofm(datem))==6 
* put cutoff value in all cells
bys datem: egen cutoff=mean(median)

generate size=1 if (F0Mcap> cutoff) & F0Mcap<. & month(dofm(datem))==6 
replace  size=0 if (F0Mcap<=cutoff) & F0Mcap<. & month(dofm(datem))==6 

* cutoffs are refreshed in June.  Between June(t) and May(t+1) keep size fixed
tsset permco datem
replace  size=L.size if size==. & month(dofm(datem))!=6 

tabulate decile size
rename decile size_decile

drop median cutoff
save $Work/RET.dta, replace
* *****************************************************
* GET IBES DATA
use  ticker permco permno comnam cname datem statpers LTG mcap if LTG<. using $Work/XSibes.dta, clear
replace LTG=LTG/100

* merge with returns; drop multiple listings
merge 1:1 permco permno datem using $Work/RET
keep if _merge==3
drop _merge
bys   datem: astile LTGALL=LTG		, nq(10)
* *****************************************************
save  $Work/RET_LTG, replace
* *****************************************************
!rm $Work/RET.dta -f
